<template>
  <div class="content-wrap">
    <el-row class="btnWrap">
      <el-button
        icon="el-icon-plus"
        class="btn left-btn"
        type="primary"
        @click="showInput"
      >
        新增
      </el-button>
    </el-row>

    <el-table fit border :data="tableList" height="500" stripe>
      <el-table-column
        label="班级ID"
        align="center"
        width="80px"
        header-align="center"
        prop="classId"
      ></el-table-column>
      <el-table-column
        label="班级名"
        align="center"
        header-align="center"
        prop="className"
      ></el-table-column>
      <el-table-column
        label="班主任"
        :formatter="mainTeacherFormatter"
        align="center"
        header-align="center"
        prop="mainTeacherId"
      ></el-table-column>
      <el-table-column
        label="年级"
        align="center"
        header-align="center"
        prop="grade"
      ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="item">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="editListItem(item.$index, item.row)"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteListItem(item.$index, item.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增 -->
    <el-dialog title="新增" :visible.sync="isShowEditAlert">
      <el-form label-width="100px" :inline="true">
        <el-form-item label="班级名" prop="className">
          <el-input v-model="form.name" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="班主任" prop="mainTeacherId">
          <el-select v-model="form.mainTeacherId" placeholder="选择性别">
            <el-option
              v-for="item in teac_list"
              :key="item.teac_id"
              :label="item.name"
              :value="item.teac_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-input v-model="form.grade" placeholder=""></el-input>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="success" @click="submitForm"> 提交 </el-button>
        <el-button type="success" @click="submitClose1"> 取消 </el-button>
      </template>
    </el-dialog>

    <el-dialog title="提示" width="30%" center :visible.sync="isShowEditAdd">
      <b>请确认是否提交！</b>
      <template slot="footer">
        <el-button type="success" @click="submitFormAdd"> 确认 </el-button>
        <el-button type="success" @click="submitClose2"> 取消 </el-button>
      </template>
    </el-dialog>

    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="isShowEditDialog">
      <el-form label-width="100px" :inline="true">
        <el-form-item label="班级名" prop="className">
          <el-input v-model="formModify.className" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="班主任" prop="mainTeacherId">
          <el-select v-model="formModify.mainTeacherId" placeholder="选择老师">
            <el-option
              v-for="item in teac_list"
              :key="item.teac_id"
              :label="item.name"
              :value="item.teac_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-input v-model="formModify.grade" placeholder=""></el-input>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="success" @click="submitFormDialog"> 提交 </el-button>
        <el-button type="success" @click="submitClose1"> 取消 </el-button>
      </template>
    </el-dialog>
    <el-dialog title="提示" width="30%" center :visible.sync="isShowEditModify">
      <b>请确认是否修改！</b>
      <template slot="footer">
        <el-button type="success" @click="submitFormModify"> 确认 </el-button>
        <el-button type="success" @click="submitClose2"> 取消 </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "api";
export default {
  name: "StudentInfo",
  data() {
    return {
      teac_list: [],
      tableList: [],
      isShowDeleteAlert: false,
      //增加
      isShowEditAlert: false,
      isShowEditAdd: false,
      //修改
      isShowEditDialog: false,
      isShowEditModify: false,
      allDevList: [],
      form: {},
      formModify: {},
    };
  },

  methods: {
    //新增 add
    submitFormAdd() {
      request({
        method: "post",
        url: "class/addClass.php",
        data: {
          name: this.form.name,
          grade: this.form.grade,
          mainTeacherId: this.form.mainTeacherId,
        },
      }).then( res => {
        console.log(res);
        if (res.state === "success") {
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.GetAllStudent();
        } else {
          this.$message({
            message: "添加失败:" + res.err,
            type: "error",
          });
          this.isShowEditAdd = false;
        }
      });
    },
    //删除 delete
    deleteListItem(index) {
      let id = this.tableList[index].stu_id;
      this.axios
        .post("class/delClass", {
          id: id,
        })
        .then(res => {
          console.log(res);
          if (res.state === "success") {
            this.$message({
              message: "删除成功",
              type: "success",
            });
            this.GetAllStudent();
          } else {
            this.$message({
              message: "删除失败:" + res.err,
              type: "error",
            });
          }
        });
    },
    //编辑 modify
    submitFormModify() {
      request({
        method: "post",
        url: "class/editClass.php",
        data: {
          className: this.formModify.className,
          classId: this.formModify.classId,
          mainTeacherId: this.formModify.mainTeacherId,
          grade: this.formModify.grade,
        },
      }).then((res) => {
        if (res.state == "success") {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.closeAllWindow();
          this.GetAllStudent();
        } else {
          this.$message({
            message: "修改失败:" + res.err,
            type: "error",
          });
          this.isShowEditModify = false;
        }
      });
    },
    //刷新
    GetAllStudent() {
      request({
        method: "get",
        url: "class/getAllClass.php",
      }).then((res) => {
        this.tableList = res.data;
        this.closeAllWindow();
      });
    },
    closeAllWindow() {
      this.isShowDeleteAlert = false;
      this.isShowEditAlert = false;
      this.isShowEditAdd = false;
      this.isShowEditDialog = false;
      this.isShowEditModify = false;
    },
    mainTeacherFormatter(row) {
      const id = row.mainTeacherId;
      let name = null;
      console.log(this.teac_list);
      this.teac_list.forEach((item) => {
        if (item.teac_id == id) {
          name = item.name;
        }
      });
      return name || "null";
    },
    //控制dialog出现消失
    showInput() {
      this.isShowEditAlert = true;
    },
    getTeacList() {
      this.axios.get("/opt/teacher", {}).then((res) => {
        console.log(res.data.data);
        this.teac_list = res.data.data;
      });
    },
    submitForm() {
      this.isShowEditAdd = true;
    },
    submitClose1() {
      this.isShowEditAlert = false;
      this.isShowEditDialog = false;
    },
    submitClose2() {
      this.isShowEditAdd = false;
      this.isShowEditModify = false;
    },
    editListItem(index) {
      this.isShowEditDialog = true;
      this.formModify = JSON.parse(JSON.stringify(this.tableList[index]));
      console.log(this.formModify);
    },
    submitFormDialog() {
      this.isShowEditModify = true;
    },
  },
  mounted() {
    this.getTeacList();
    this.GetAllStudent();
  },
};
</script>

<style scoped>
.content-wrap .btn {
  position: absolute;
  z-index: 100;
}
.content-wrap .btnWrap {
  position: relative;
  height: 50px;
}
.content-wrap .left-btn {
  left: 0;
}
.content-wrap .right-btn {
  right: 0;
}
</style>